;;; init-ui.el --- my emacs ui init file

;;; Commentary:

;;; Code:

(let ((class '((class color) (min-colors 89)))
      ;; mark palette
      (mark-white          "#ffffff")
      (mark-grey-3         "#cecece")
      (mark-dark           "#000000")
      )
;; Init Theme
(add-to-list 'custom-theme-load-path "~/.emacs.d/elpa/themes/molokai-theme")

;; Init popwin mode
(require 'popwin)
(popwin-mode 1)

;;Init neotree
(add-to-list 'load-path "~/.emacs.d/elpa/neotree")
(require 'neotree)

;;Highlight print
(global-font-lock-mode 1)
(transient-mark-mode t)

;;Cursor type
;;(setq-default cursor-type 'bar)

;;Set frame
;;  (set-frame-position (selected-frame) 280 30)
;;  (set-frame-width (selected-frame) 150)
;;  (set-frame-height (selected-frame) 45)

;;visual-line-mode
(global-visual-line-mode 1)

;; Windows number mode
(window-numbering-mode 1)

;;anzu
(require 'anzu)
(global-anzu-mode +1)

(set-face-attribute 'anzu-mode-line nil
                    :foreground "yellow" :weight 'bold)

(custom-set-variables
 '(anzu-mode-lighter "")
 '(anzu-deactivate-region t)
 '(anzu-search-threshold 1000)
 '(anzu-replace-threshold 50)
 '(anzu-replace-to-string-separator " => "))

(define-key isearch-mode-map [remap isearch-query-replace]  #'anzu-isearch-query-replace)
(define-key isearch-mode-map [remap isearch-query-replace-regexp] #'anzu-isearch-query-replace-regexp)

;;mode line
(defun mark/update-persp-name ()
  (when (bound-and-true-p persp-mode)
    ;; There are multiple implementations of
    ;; persp-mode with different APIs
    (progn
      (or (not (string= persp-nil-name (safe-persp-name (get-frame-persp))))
	  "Default")
      (let ((name (safe-persp-name (get-frame-persp))))
	(propertize (concat "[" name "] ")
		    'face 'font-lock-preprocessor-face
		    'help-echo "Current Layout name.")))))


(defun spaceline--unicode-number (str)
  "Return a nice unicode representation of a single-digit number STR."
  (cond
;;   ((string= "1" str) "➊")
;;   ((string= "2" str) "➋")
;;   ((string= "3" str) "➌")
;;   ((string= "4" str) "➍")
;;   ((string= "5" str) "➎")
;;   ((string= "6" str) "➏")
;;   ((string= "7" str) "➐")
;;   ((string= "8" str) "➑")
;;   ((string= "9" str) "➒")
;;   ((string= "0" str) "➓")))
   ((string= "1" str) "Ⅰ")
   ((string= "2" str) "Ⅱ")
   ((string= "3" str) "Ⅲ")
   ((string= "4" str) "Ⅳ")
   ((string= "5" str) "Ⅴ")
   ((string= "6" str) "Ⅵ")
   ((string= "7" str) "Ⅶ")
   ((string= "8" str) "Ⅷ")
   ((string= "9" str) "Ⅸ")
   ((string= "0" str) "Ⅹ")))

(defun window-number-mode-line ()
  "The current window number. Requires 'window-numbering-mode' to be enabled."
  (when (bound-and-true-p window-numbering-mode)
    (let* ((num (window-numbering-get-number))
	   (str (when num (int-to-string num))))
      (spaceline--unicode-number str))))

(defun mode-line-fill (face reserve)
  "Return empty space using FACE and leaving RESERVE space on the right."
  (unless reserve
    (setq reserve 20))
  (when (and window-system (eq 'right (get-scroll-bar-mode)))
    (setq reserve (- reserve 3)))
  (propertize " "
	      'display `((space :align-to
				(- (+ right right-fringe right-margin) ,reserve)))
	      ;;                'face face
	      ))

(defun buffer-encoding-abbrev ()
  "The line ending convention used in the buffer."
  (let ((buf-coding (format "%s" buffer-file-coding-system)))
    (if (string-match "\\(dos\\|unix\\|mac\\)" buf-coding)
	(match-string 1 buf-coding)
      buf-coding)))

(setq my-flycheck-mode-line
      '(:eval
	(pcase flycheck-last-status-change
	  (`not-checked nil)
	  (`no-checker (propertize " -" 'face 'warning))
	  (`running (propertize " ✷" 'face 'success))
	  (`errored (propertize " !" 'face 'error))
	  (`finished
	   (let* ((error-counts (flycheck-count-errors flycheck-current-errors))
		  (no-errors (cdr (assq 'error error-counts)))
		  (no-warnings (cdr (assq 'warning error-counts)))
		  (face (cond (no-errors 'error)
			      (no-warnings 'warning)
			      (t 'success))))
	     (propertize (format "[%s/%s]" (or no-errors 0) (or no-warnings 0))
			 'face face)))
	  (`interrupted " -")
	  (`suspicious '(propertize " ?" 'face 'warning)))))

(setq-default mode-line-format
	      (list
	       " %1"
	       '(:eval (propertize
			(window-number-mode-line)
			'face '(( :foreground ,mark-white))
			))

	       '(:eval (mark/update-persp-name))
	       "%1 "
	       
	       ;; the buffer name; the file name as a tool tip
	       "|"
	       '(:eval (propertize "%b"
				   'face '((:foreground ,mark-dark))
				   ;;				     'font-lock-keyword-face
				   'help-echo (buffer-file-name)))
	       "|"
	       
	       
	       ;; anzu
	       anzu--mode-line-format
	       
	       ;; relative position, size of file
	       " "
	       (propertize "%p"
			   'face '(( :foreground ,mark-dark))
			   ) ;; % above top
	       
	       " "
	       (propertize "%I"
			   'face '((:foreground ,mark-dark))
			   ) ;; size
	       "  "
	       
	       ;; the current major mode for the buffer.
	       
	       '(:eval (propertize "%m" 'face '(( :foreground "purple"))
				   'help-echo buffer-file-coding-system))

	       ;; minor modes
;;	       minor-mode-alist
;;	       '(:eval  (propertize "%" 'face '((:background "green" :foreground "balck"))
;;				    'help-echo "Buffer has been modified"))
      
	       ;; git info
;;	       '(vc-mode vc-mode)
	       
	       ;; global-mode-string goes in mode-line-misc-info
	       mode-line-misc-info
	       
	       (mode-line-fill 'mode-line 28)
      
	       "%1"
	       my-flycheck-mode-line
	       "%1"
	       ;; evil state
	       ;;'(:eval evil-mode-line-tag)
	       
	       (mode-line-fill 'mode-line 23)
	       
	       ;; was this buffer modified since the last save?
	       '(:eval (when (buffer-modified-p)
			 (
			  concat " "
				 (propertize " M "
					     'face '((:background "orange" :foreground "black"))
					     'help-echo "Buffer has been modified"))))
	       
	       (mode-line-fill 'mode-line 19)

	       ;; insert vs overwrite mode, input-method in a tooltip
	       '(:eval (propertize (if overwrite-mode " O " " I ")
				   'face '((:background "lightgreen" :foreground "brown"))
				   'help-echo (concat "Buffer is in "
						      (if overwrite-mode
							  "overwrite"
							"insert") " mode")))
	       
	       (mode-line-fill 'mode-line 17)
	       
	       ;; is this buffer read-only?
	       '(:eval (when buffer-read-only
			 (
			  concat ""
				 (propertize " R "
					     'face '((:background "#FF0000" :foreground "black"))
					     'help-echo "Buffer is read-only"))))
	       
	       (mode-line-fill 'mode-line 13)
	       
	       ;; line and column
	       "[" ;; '%02' to set to 2 chars at least; prevents flickering
	       ;;                 (propertize "%02l"
	       ;;                 	'face 'font-lock-type-face
	       ;;                 	) ","
	       ;;                ") "
	       
	       ;;		 (mode-line-fill 'mode-line 8)
	       ;;		 " "
	       (propertize "%03c"
			   'face 'font-lock-type-face
			   )
	       "] "
	       '(:eval (buffer-encoding-abbrev))
	       mode-line-end-spaces
	       ;; add the time, with the date and the emacs uptime in the tooltip
	       ;; '(:eval (propertize (format-time-string "%H:%M")
	       ;;                     'help-echo
	       ;;                     (concat (format-time-string "%c; ")
	       ;;                             (emacs-uptime "Uptime:%hh"))))
	       ))

;;(set-background-color mark-grey-3)
;;(set-fringe-style)
)

(provide 'init-ui)
;;; init-ui.el ends here
